import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AppService2 } from './app.service2';
import { DemoController } from './demo/demo.controller';
import { DemoModule } from './demo/demo.module';
import { UserModule } from './user/user.module';
import { ListModule } from './list/list.module';
import { ConfigMoudle } from './config/config.moudle'; //自定义读取配置 
import { UploadModule } from './upload/upload.module';
import { PModule } from './p/p.module';
import { LoginModule } from './login/login.module';
import { SpiderModule } from './spider/spider.module';
import { GuardModule } from './guard/guard.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TestModule } from './test/test.module';
import { MangerModule } from './manger/manger.module';
import { JwtAuthGuard } from './common/guards/auth.guard'
import { APP_GUARD } from '@nestjs/core'
import { AuthModule } from './auth/auth.module';
import { ConfigModule } from '@nestjs/config';
import { WebsocketModule } from './websocket/websocket.module';

@Module({
  imports: [WebsocketModule,ConfigModule.forRoot(),DemoModule, UserModule, ListModule, ConfigMoudle.forRoot({
     path:"/xiaoman"
  }),
  TypeOrmModule.forRoot({
    type: "mysql", //数据库类型
    username: "root", //账号
    password: "1234", //密码
    host: "localhost", //host
    port: 3306, //
    database: "db", //库名
    // entities: [__dirname + '/**/*.entity{.ts,.js}'], //实体文件
    synchronize:true, //synchronize字段代表是否自动将实体类同步到数据库
    retryDelay:500, //重试连接数据库间隔
    retryAttempts:10,//重试连接数据库的次数
    autoLoadEntities:true, //如果为true,将自动加载实体 forFeature()方法注册的每个实体都将自动添加到配置对象的实体数组中
  }), 
  UploadModule, 
  PModule, 
  AuthModule,//jwt鉴权模块
  LoginModule, 
  SpiderModule, 
  GuardModule, 
  TestModule, 
  MangerModule],
  controllers: [AppController, DemoController],
  providers: [AppService2,{ //service自定义名称
    provide:"ABC",
    useClass:AppService
  },
  {
    provide:"Test",  //service 自定义注入值
    useValue:["TB","PDD","JD"]
  },
  {
    provide:"CCC",  //service 工厂模式
    inject:[AppService2],
    async useFactory(AppService2:AppService2){ //service 异步模式
      return await new Promise((r)=>{
        setTimeout(()=>{
          r(AppService2.getHello());
        },5000);
      });
    }
    // useFactory(AppService2:AppService2){
    //   console.log(AppService2.getHello());
    //   return 123
    // }
  },
  {
    provide: APP_GUARD,
    useClass: JwtAuthGuard, //jwt全局守卫
  }],
})
export class AppModule {}

